<?php

class PeopleController extends FrontEndController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view', 'getallfriends', 'getonlinefriends', 'getrequestfriends'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionIndex()
	{
		$tabs = array(
			'allFriends',
			'onlineFriends',
			'requestFriends'
		);
		
		$limit = 10;
		
		if(isset($_GET['tab']) && in_array($_GET['tab'], $tabs)){
			$current_tab = $_GET['tab'];
		}else{
			$current_tab = 'allFriends';
		}
		
		if(isset($_REQUEST['text'])){
			$search = $_REQUEST['text'];
		}else{
			$search = '';
		}
		
		if(isset($_GET['profile'])){
			$profile = $_GET['profile'];
		}else{
			$profile = '';
		}		
		
		if($current_tab == 'allFriends'){
			$search = addcslashes($search, '%_');
			$filter = array(
				'status' => 1,
				'filter_name' => $search,
				'user_id' => $profile,
				'limit'=>$limit
			);
			$data = Friends::model()->searchFriends($filter);
			$countData = Friends::model()->countFriends($filter);
		}elseif($current_tab == 'onlineFriends'){
			$search = addcslashes($search, '%_');
			$filter = array(
				'status' => 1,
				'filter_name' => $search,
				'filter_online' => true,
				'user_id' => $profile,
				'limit'=>$limit
			);
			$data = Friends::model()->searchFriends($filter);
			$countData = Friends::model()->countFriends($filter);
		}elseif($current_tab == 'requestFriends'){
			$search = addcslashes($search, '%_');
			$filter = array(
				'status' => 0,
				'filter_name' => $search,
				'friend_id' => $profile,
				'limit'=>$limit
			);
			$data = Friends::model()->searchFriends($filter);
			$countData = Friends::model()->countFriends($filter);
		}
		
		$countries = Country::model()->findAll();
		
		$this->render('index', array(
			'current_tab'=>$current_tab,
			'search'=>$search,
			'data'=>$data,
			'countData'=>$countData,
			'countries'=>$countries,
			'profile'=>$profile,
			'limit'=>$limit,
			'offset'=>$limit
		));		
	}

	public function actionGetAllFriends(){
		if (Yii::app()->request->isAjaxRequest) {
			$offset = 0;
		
			if(!empty($_GET['offset'])){
				$offset=$_GET['offset'];
				$new_offset = $_GET['offset'];
			}else{
				$new_offset = 0;
			}
	
			if(!empty($_GET['limit'])){
				$limit = $_GET['limit'];
				$new_offset = $offset + $_GET['limit'];
			}else{
				$limit = 10;
				$new_offset = $offset + $limit;
			}
				
			if(!empty($_GET['filter_country'])){
				$country_id=$_GET['filter_country'];
			}else{
				$country_id=false;
			}
	
			if(!empty($_GET['filter_age_from'])){
				$filter_age_from = $_GET['filter_age_from'];
			}else{
				$filter_age_from = false;
			}
	
			if(!empty($_GET['filter_age_to'])){
				$filter_age_to = $_GET['filter_age_to'];
			}else{
				$filter_age_to = false;
			}
				
			if(!empty($_GET['filter_sex'])){
				$sex = $_GET['filter_sex'];
			}else{
				$sex = false;
			}
				
			if(!empty($_REQUEST['text'])){
				$search = $_REQUEST['text'];
			}else{
				$search = '';
			}

			if(isset($_GET['profile'])){
				$profile = $_GET['profile'];
			}else{
				$profile = '';
			}			
			
			$search = addcslashes($search, '%_');
			$filter = array(
				'status' => 1,
				'filter_name' => $search,
				'user_id' => $profile,
				'country_id'=>$country_id,
				'sex' => $sex,
				'filter_age_to'=>$filter_age_to,
				'filter_age_from'=>$filter_age_from,
				'offset'=>$offset,
				'limit'=>$limit			
			);
			$data = Friends::model()->searchFriends($filter);
	
			$this->renderPartial('_allFriendsList',array(
				'people' => $data,
				'limit' => $limit,
				'offset' => $new_offset,
				'profile'=>$profile
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}
	}
	
	public function actionGetOnlineFriends(){
		if (Yii::app()->request->isAjaxRequest) {
			$offset = 0;
	
			if(!empty($_GET['offset'])){
				$offset=$_GET['offset'];
				$new_offset = $_GET['offset'];
			}else{
				$new_offset = 0;
			}
	
			if(!empty($_GET['limit'])){
				$limit = $_GET['limit'];
				$new_offset = $offset + $_GET['limit'];
			}else{
				$limit = 10;
				$new_offset = $offset + $limit;
			}
	
			if(!empty($_GET['filter_country'])){
				$country_id=$_GET['filter_country'];
			}else{
				$country_id=false;
			}
	
			if(!empty($_GET['filter_age_from'])){
				$filter_age_from = $_GET['filter_age_from'];
			}else{
				$filter_age_from = false;
			}
	
			if(!empty($_GET['filter_age_to'])){
				$filter_age_to = $_GET['filter_age_to'];
			}else{
				$filter_age_to = false;
			}
	
			if(!empty($_GET['filter_sex'])){
				$sex = $_GET['filter_sex'];
			}else{
				$sex = false;
			}
	
			if(!empty($_REQUEST['text'])){
				$search = $_REQUEST['text'];
			}else{
				$search = '';
			}
	
			if(isset($_GET['profile'])){
				$profile = $_GET['profile'];
			}else{
				$profile = '';
			}
				
			$search = addcslashes($search, '%_');
			$filter = array(
					'status' => 1,
					'filter_name' => $search,
					'filter_online'=>true, 
					'user_id' => $profile,
					'country_id'=>$country_id,
					'sex' => $sex,
					'filter_age_to'=>$filter_age_to,
					'filter_age_from'=>$filter_age_from,
					'offset'=>$offset,
					'limit'=>$limit
			);
			$data = Friends::model()->searchFriends($filter);
	
			$this->renderPartial('_onlineFriendsList',array(
					'people' => $data,
					'limit' => $limit,
					'offset' => $new_offset,
					'profile'=>$profile
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}
	}	
	
	public function actionGetRequestFriends(){
		if (Yii::app()->request->isAjaxRequest) {
			$offset = 0;
	
			if(!empty($_GET['offset'])){
				$offset=$_GET['offset'];
				$new_offset = $_GET['offset'];
			}else{
				$new_offset = 0;
			}
	
			if(!empty($_GET['limit'])){
				$limit = $_GET['limit'];
				$new_offset = $offset + $_GET['limit'];
			}else{
				$limit = 10;
				$new_offset = $offset + $limit;
			}
	
			if(!empty($_GET['filter_country'])){
				$country_id=$_GET['filter_country'];
			}else{
				$country_id=false;
			}
	
			if(!empty($_GET['filter_age_from'])){
				$filter_age_from = $_GET['filter_age_from'];
			}else{
				$filter_age_from = false;
			}
	
			if(!empty($_GET['filter_age_to'])){
				$filter_age_to = $_GET['filter_age_to'];
			}else{
				$filter_age_to = false;
			}
	
			if(!empty($_GET['filter_sex'])){
				$sex = $_GET['filter_sex'];
			}else{
				$sex = false;
			}
	
			if(!empty($_REQUEST['text'])){
				$search = $_REQUEST['text'];
			}else{
				$search = '';
			}
	
			if(isset($_GET['profile'])){
				$profile = $_GET['profile'];
			}else{
				$profile = '';
			}
	
			$search = addcslashes($search, '%_');
			$filter = array(
				'status' => 0,
				'filter_name' => $search,
				'friend_id' => $profile,
				'country_id'=>$country_id,
				'sex' => $sex,
				'filter_age_to'=>$filter_age_to,
				'filter_age_from'=>$filter_age_from,
				'offset'=>$offset,
				'limit'=>$limit
			);
			$data = Friends::model()->searchFriends($filter);
	
			$this->renderPartial('_onlineFriendsList',array(
				'people' => $data,
				'limit' => $limit,
				'offset' => $new_offset,
				'profile'=>$profile
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}
	}	

}
